package edu.hitsz.Rank;

import java.io.*;
import java.util.ArrayList;
import java.util.Comparator;

public class RankListDAOMPL implements RankListDAO, Serializable {
    private ArrayList<RankObjcet> ranks;
    @Override
    public ArrayList<RankObjcet> getAllRanks() {
        return ranks;
    }

    static File srcFile;

    @Override
    public void doAdd(RankObjcet rank) {
        if (ranks == null) {
            ranks = new ArrayList<RankObjcet>();
        }
        ranks.add(rank);
        ranks.sort(Comparator.comparing(RankObjcet::getScore).reversed());
        for (int i = 0; i < ranks.size(); i++) {
            ranks.get(i).setRank(i+1);
        }
    }

    public void doDelete(int rank) {
        if (ranks == null) {
            ranks = new ArrayList<RankObjcet>();
        }
        ranks.remove(rank);
        ranks.sort(Comparator.comparing(RankObjcet::getScore).reversed());
        for (int i = 0; i < ranks.size(); i++) {
            ranks.get(i).setRank(i+1);
        }

        RankListDAO rankListDAO = new RankListDAOMPL();
        rankListDAO.setAllRanks(ranks);
        ObjectOutputStream out = null;
        try {
            out = new ObjectOutputStream(new FileOutputStream(srcFile));
            out.writeObject(rankListDAO);
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }


    public static void setPath(int mode) {
        if (mode==0) {
            srcFile = new File("E:\\IDEAProjects\\AircraftWar-base\\data\\rank_easy.dat");
        } else if (mode==1) {
            srcFile = new File("E:\\IDEAProjects\\AircraftWar-base\\data\\rank_normal.dat");
        }  else if (mode==2) {
            srcFile = new File("E:\\IDEAProjects\\AircraftWar-base\\data\\rank_hard.dat");
        }
    }

    @Override
    public void setAllRanks(ArrayList<RankObjcet> ranks) {
        this.ranks = ranks;
    }

    // TODO RankDAO
    public RankListDAOMPL ranking(String name, int score) {
        // 读取文件
        if (!srcFile.exists()) {
            try {
                srcFile.createNewFile();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }

        // 创建DAOMPL对象并反序列化存储的数据
        RankListDAO rankListDAO = new RankListDAOMPL();
        ObjectInputStream in = null;
        try {
            in = new ObjectInputStream(new FileInputStream(srcFile));
            rankListDAO = (RankListDAOMPL) in.readObject();
        } catch (Exception e) {
            e.printStackTrace();
        }

        // 加入数据并输出
        RankObjcet rank = new RankObjcet(name, score);
        rankListDAO.doAdd(rank);
        ranks = rankListDAO.getAllRanks();
//        System.out.println("***********\nRankingList\n***********");
//        for (RankObjcet r: ranks){
//            System.out.println("rank:" + r.getRank() + " name: "+r.getUserName()+" score: "+r.getScore()+ " time: " + r.getTime());
//        }

        // 写入文件
        ObjectOutputStream out = null;
        try {
            out = new ObjectOutputStream(new FileOutputStream(srcFile));
            out.writeObject(rankListDAO);
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }

        return (RankListDAOMPL) rankListDAO;
    }
}
